import globalConfig from '../config'
import util from '@/libs/util'

const env = process.env.NODE_ENV
const baseHttp = globalConfig.API_HOST

const apiVersion = globalConfig.API_VERSION //  '/v1';
const apiSecret = globalConfig.API_SECRET

var api = {
	addUrlParts(url, urlParams) {
		var arr = []
		for (var key in urlParams) {
			arr.push(key + '=' + urlParams[key])
		}
		return url + '?' + arr.join('&')
	},
	batch: (batchParams) => {
		return new Promise((resolve, reject) => {

		})
	},
	get: (url, getParams) => {
		return new Promise((resolve, reject) => {
			//let token = util.isUserLogin()
			let header = {}
			/*
                        if (token) {
                            getParams['token'] = token
                        }
            */
			const accountInfo = uni.getAccountInfoSync();
			getParams['appId'] = accountInfo.miniProgram.appId

			let baseHttp = baseHttp + url
			uni.request({
				method: 'GET',
				url: url, //仅为示例，并非真实接口地址。
				data: getParams,
				header: header,
				success: (res) => {
					/*
					if (globalConfig.DEBUG) {
						console.log(res);
					}
					*/
					//this.text = 'request success';
					resolve(res.data)
				},
				fail: (error) => {
					reject(error)
				}
			});
		})
	},
	post: (url, postParams) => {
		if (globalConfig.DEBUG) {
			// console.log(url)
			// console.log(postParams)
			//util.sleep(5000)
		}

		return new Promise((resolve, reject) => {

			let runAxios = true

			if (globalConfig.USE_MOCK) {
				let getApiData = require('./mock/async.js').default
				let apiData = getApiData(url)
				apiData = util.toNormalArr(apiData);
				if (apiData) {
					resolve(apiData);
					runAxios = false
				}
			}

			if (runAxios) {
				let token = util.isUserLogin()
				let header = {}
				if (token) {
					//header['token'] = token
				}
				header['Content-Type'] = 'application/x-www-form-urlencoded';

				//postParams['apiVersion'] =  apiVersion
				if (token) {
					postParams['token'] = token
				}
				const accountInfo = uni.getAccountInfoSync();
				postParams['appId'] = accountInfo.miniProgram.appId
				url = baseHttp + url
				uni.request({
					method: 'POST',
					url: url, //仅为示例，并非真实接口地址。
					data: postParams,
					header: header,
					success: (res) => {
						/*
						if (globalConfig.DEBUG) {
							console.log(res);
						}
						*/
						if (res.data.msg == "请先登录") {
							console.log('请先登录: ')
							global.setToken(null)
							//跳转到登录页

							/*
							uni.navigateTo({
								url: "/pages/user/auth/auth"
							})
							*/

						} else {
							//this.text = 'request success';
							resolve(res.data)
						}

						//判断是否需要刷新token

						//this.text = 'request success';
						//resolve(res.data)

					},
					fail: (error) => {
						reject(error)
					}
				});
			}
		})
	},
	search: (baseUrl, urlParams, postParams) => {

		if (globalConfig.DEBUG) {
			// console.log(baseUrl)
			// console.log(urlParams)
			// console.log(postParams)
		}

		return new Promise((resolve, reject) => {

			let runAxios = true

			if (globalConfig.USE_MOCK) {
				//util.sleep(3000)
				let getApiData = require('./mock/async.js').default
				let apiData = getApiData(baseUrl)
				if (apiData) {
					apiData = util.toNormalArr(apiData);
					resolve(apiData);
					runAxios = false
				}
			}

			if (runAxios) {
				let token = util.isUserLogin()
				let header = {}
				if (token) {
					header['token'] = token
				}
				postParams['apiVersion'] =  apiVersion
				let url = api.addUrlParts(baseUrl, urlParams)
				url = baseHttp + url
				uni.request({
					method: 'POST',
					url: url, //仅为示例，并非真实接口地址。
					data: postParams,
					header: header,
					success: (res) => {
						if (globalConfig.DEBUG) {
							// console.log(res.data);
						}

						//判断是否需要刷新token
						if (res.data.code == 40010 || res.data.code == 40011) {
							global.setToken(null)
							// var jump = uni.getStorageSync('isJump')
							// console.log('jump: ', jump)
							// if (!jump) {
							// 	//跳转到登录页
							// 	uni.navigateTo({
							// 		url: "/pages/user/auth/auth"
							// 	})
							// }
							// uni.setStorageSync('isJump', 'true')

							//跳转到登录页
							uni.navigateTo({
								url: "/pages/user/auth/auth"
							})
						} else {
							//this.text = 'request success';
							resolve(res.data)
						}
					},
					fail: (error) => {
						reject(error)
					}
				});
			}
		})
	}
}

export default api
